Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
req.params is throwing "undefined" in Router file

I am doing a practise project in Node/Express.

My server.js looks like this:

const express = require("express")
const app = express()
const bodyParser = require("body-parser")
const morgan = require("morgan")
const { get } = require("http")

const PORT = 3000

const budgetRoute = require("./routes/Budget")
const envelopeRoute = require("./routes/Envelopes")
const envelopeNameRoute =require("./routes/Envelopes_name")
const envelopeTransferRoute =require("./routes/Envelopes_transfer")

global.envelopes = []
global.totalBudget = 0

app.use(bodyParser.json())
app.use(morgan("dev"))

app.use("/envelopes", envelopeRoute)
app.use("/envelopes/:name", envelopeNameRoute)
app.use("/envelopes/transfer/:name", envelopeTransferRoute)
app.use("/budget", budgetRoute)


app.listen(PORT, () =>{
    console.log(`Listening on port ${PORT}`)
})

now in my Route file routes/Envelopes_name I have this:

router.put("/", (req,res,next)=>{
    const envelopeToChangeName = req.params.name
    const envelopeName = req.query.name
    const envelopeBudget = req.query.budget
    const reqEnvelope = envelopes.find(envelope => envelopeToChangeName)
    if(envelopeBudget){
        reqEnvelope.budget = envelopeBudget 
    }
    if(envelopeName){
        reqEnvelope.name = envelopeName
    }
    res.status(201).send(reqEnvelope)
})

after sending request localhost:3000/envelopes/groceries?name=taxes it should change the name of the envelope from "groceries" to "taxes". For some reason req.params.name is "undefined" and I have error "TypeError: Cannot set properties of undefined (setting 'name')". When I had it all in one file, without routers, it worked perfectly. Any ideas?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think you need to add { mergeParams: true } to make the params available, because you use another router file then where you define the route.

See Express docs

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!